import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_app_notice_entity.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'msg_list_logic.dart';

/**
 * @Description: 消息中心
 * @Author: 下雨了
 * @CreateDate: 2021-11-05 15:17
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-7-1 09:10:04
 * @UpdateRemark: getx
 * @Version: 1.1
 */
///
class MsgListPage extends StatefulWidget {
  final String  title;
  const MsgListPage({Key? key, required this.title}) : super(key: key);


  @override
  _MsgListPageState createState() => _MsgListPageState();
}

class _MsgListPageState extends State<MsgListPage> {
  final logic = Get.put(MsgListLogic());
  final state = Get.find<MsgListLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MsgListLogic>(builder: (logic) {
      return Scaffold(
        appBar: MyAppBar(context, title: "消息",
          isShowLeading: false,
          actions: [
            Container(
              margin: EdgeInsets.only(right: 16.r),
              child: TextButton(
                child: Text("标记已读", style: TextStyle(color: Colors.white, fontSize: 14.sp),),
                onPressed: () {
                  logic.updateAllNoticeRead();
                }
              ),
            ),
          ]
        ),
        body: Column(
          children: [
            Visibility(
              visible: state.notification,
              child: Container(
                padding: EdgeInsets.only(left: 16.r, right: 16.r),
                height: 35.r,
                color: MyColors.FFFDEBEA,
                child: Row(
                  children: [
                    Expanded(child: Text("开启消息通知，不错过重要信息!", style: TextStyle(color: MyColors.FFF03F35, fontSize: 13.sp),)),
                    TextButton(
                      style: ButtonStyle(
                        padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                        shape: MaterialStateProperty.all(
                          StadiumBorder(
                            side: BorderSide(
                                style: BorderStyle.none
                            ),
                          ),
                        ),
                        //设置按钮的大小
                        minimumSize: MaterialStateProperty.all(Size(65.r, 25.r)),
                        backgroundColor: MaterialStateProperty.all<Color>(MyColors.FFF03F35),
                      ),
                      child: Text("去开启", style: TextStyle(color: Colors.white, fontSize: 12.sp),),
                      onPressed: () {
                        logic.setNotification(false);
                        logic.openNotificationSettings();
                      },
                    )
                  ],
                ),
              ),
            ),
            Expanded(
              child: SmartRefresher(
                controller: logic.refreshController,
                //  允许下拉刷新
                enablePullDown: true,
                // 允许上拉加载
                enablePullUp: true,
                // 刷新
                onRefresh: logic.onRefresh,
                // 加载
                onLoading: logic.onLoading,
                header: WaterDropHeader(complete: Text('刷新完成', style: TextStyle(color: MyColors.FF333333))),
                footer: ClassicFooter(loadingText: '加载中...', idleText: '上拉加载更多..', noDataText: '没有更多数据了'),
                child: (state.listAppNoticeEntity?.content?.length ?? 0) > 0 ? ListView.builder(
                  itemCount: state.listAppNoticeEntity?.content?.length,
                  physics: BouncingScrollPhysics(),
                  itemBuilder: (BuildContext context, int index){
                    return _MsgListItem(state.listAppNoticeEntity!.content![index], index);
                  },
                ) : EmptyView(),
              ),
            ),
          ],
        ),

      );
    });
  }

  /// item 列表
  Widget _MsgListItem(ListAppNoticeContent item, int index){
    return InkWell(
      child: Container(
        padding: EdgeInsets.only(left: 16.r, top: 15.r, right: 16.r),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                    margin: EdgeInsets.only(left: 4.r, right: 7.r),
                    child: Image.asset(logic.getImageRid(item.type), width: 16.r, height: 16.r,)
                ),
                Expanded(
                  child: Row(
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 5.r,right: 4.r),
                        child: Text(item.labelName ?? "",
                          style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp, fontWeight: FontWeight.w600),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Visibility(
                        visible: item.isRead == '0',
                        child: Container(
                          width: 5.r,
                          height: 5.r,
                          decoration: BoxDecoration(
                              color: MyColors.FFF03F35,
                              borderRadius: BorderRadius.circular(5.r)
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Text(item.createTime ?? "", style: TextStyle(color: MyColors.FF999999, fontSize: 12.sp),)
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 6.r, bottom: 14.r),
              child: Text(
                // "Text(item.createTime.toString(), style: TextStyle(color: MyColors.FF999999, fontSize: 12),)",
                item.noticeName ?? "",
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: MyColors.FF666666, fontSize: 13.sp),
              ),
            ),
            Divider(
              color: MyColors.FFDDDDDD,
              thickness: 0.5,
              height: 0,
            ),
          ],
        ),
      ),
      onTap: (){
        logic.goToPage(item, index);
      },
    );
  }

  @override
  void dispose() {
    Get.delete<MsgListLogic>();
    super.dispose();
  }
}
